<?php

namespace thinkct\library\draw;

use app\admin\model\ai\Config as ConfigModel;
use app\common\model\ai\draw\Content as drawContentModel;
use draw\Yjai as YjaiService;

class Yjai
{
    protected array $config = [];
    protected mixed $yjai;

    public function __construct()
    {
        $this->config = [
            'AK' => ConfigModel::where('name', 'yjai_key')->value('value'),
            'SK' => ConfigModel::where('name', 'yjai_secret')->value('value'),
        ];
        $this->yjai = new YjaiService($this->config);
    }

    public function start_task($data = []): array
    {
        $data['prompt'] = $data['prompt'] ?? request()->post('prompt'); // 指令
        $data['ratio'] = request()->post('ratio'); // 画布比例
        $data['style'] = request()->post('style'); // 风格
        $data['init_image'] = request()->post('init_image'); // 参考图（链接）
        $data['init_strength'] = request()->post('init_strength', 50); // 参考图强度

        $result = $this->yjai->put_task(
            $data['prompt'],
            $data['ratio'],
            $data['style'],
            10,
            'stable_diffusion',
            'http://47.111.11.42:8082/painting-open-api/site/callback_task', // 回调地址
            'end',
            false,
            false,
            $data['init_image'],
            $data['init_strength']
        );
        //die(var_dump($result));
        if($result['status'] === 0) {
            return [
                'status' => 1,
                'msg' => $result['reason'] ?? '',
                'id' => $result['data']['Uuid']
            ];
        } else {
            return [
                'status' => 0,
                'msg' => $result['reason'] ?? ''
            ];
        }
    }

    public function task_detail($data): array
    {
        $result = $this->yjai->show_task($data['message_id']);
        if($result['status'] === 0) {
            // 进度Progress
            if(empty($result['data']['ImagePath'])) {
                // 等待绘画完成
                return [
                    'status' => 0,
                    'id' => $result['data']['Uuid'],
                    'progress' => $result['data']['Progress'],
                ];
            } else {
                // 保存图片到本地
                $path = '/storage/ai/draw/'.time().'.png';
                $image = file_get_contents($result['data']['ImagePath']);
                // 创建目录
                if(!is_dir(root_path() . '/public/storage/ai/draw/')) {
                    mkdir(root_path() . '/public/storage/ai/draw/', 0777, true);
                }
                file_put_contents(root_path() . 'public/' . $path, $image);
                return [
                    'status' => 1,
                    'id' => $result['data']['Uuid'],
                    'progress' => $result['data']['Progress'],
                    'url' => $path,
                ];
            }
        } else {
            return [
                'status' => 2,
                'msg' => $result['reason'] ?? '',
            ];
        }
    }
}